package cn.stylefeng.guns.modular.Dishes.controller;


import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.Dishes.param.dishesParam;
import cn.stylefeng.guns.modular.Dishes.service.dishesService;
import cn.stylefeng.guns.modular.User.param.UserParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
public class dishesController {

    @Resource
    private dishesService dishesService;
    @Value("${file.path}")
    private String filePath;

    @GetMapping("/dishes/list")
    @BusinessLog(title = "dishes_增加_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(dishesParam dishesParam){
        return new SuccessResponseData(dishesService.list(dishesParam));
    }

    @PostMapping("/dishes/add")
    @BusinessLog(title = "dishes_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(dishesParam.add.class) dishesParam dishesParam,MultipartFile avatar) throws IOException {

        String extName = avatar.getOriginalFilename().substring(avatar.getOriginalFilename().lastIndexOf("."));
        String fileName = UUID.randomUUID().toString()+extName;

        FileCopyUtils.copy(avatar.getInputStream(),new FileOutputStream(new File(filePath+fileName)));

        dishesParam.setAvatar(fileName);
        dishesService.add(dishesParam);
        return new SuccessResponseData();
    }

    @PostMapping("/dishes/delete")
    @BusinessLog(title = "dishes_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(dishesParam.delete.class) dishesParam dishesParam) {
        dishesService.delete(dishesParam);
        return new SuccessResponseData();
    }

    @PostMapping("/dishes/edit")
    @BusinessLog(title = "dishes_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(UserParam.edit.class) dishesParam dishesParam) {
        dishesService.edit(dishesParam);
        return new SuccessResponseData();
    }

    @GetMapping("/dishes/sort")
    @BusinessLog(title = "dishes_菜品分值排序", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData lookWaiterScoreSort(int day){

        return new SuccessResponseData(dishesService.lookDishesScore(day));
    }

    @PostMapping("/dishes/changeStatus")
    @BusinessLog(title = "dishes_修改状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData changeStatus(@RequestBody @Validated(dishesParam.changeStatus.class) dishesParam dishesParam) {
        dishesService.changeStatus(dishesParam);
        return new SuccessResponseData();
    }


}
